﻿<%@ page language="java" import="java.util.*,cn.ac.cintcm.dataconverge.util.Constants,com.hs.core.utils.*" pageEncoding="UTF-8"%>
<%@ page import ="cn.ac.cintcm.dataconverge.researcher.domain.*,cn.ac.cintcm.dataconverge.researchsubject.domain.*,com.hs.core.page.PageUtil,com.hs.core.utils.StringUtilTool" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String imagePath = path + "/index/";
Subject subject = (Subject)request.getAttribute("subject");
List orgList = (List)request.getAttribute("orgList");
Long  currentOrgId = -1l;
if(request.getAttribute("currentOrgId") != null)
  currentOrgId = (Long)request.getAttribute("currentOrgId");
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>DataConverge</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <script language="javascript" src="<%=path %>/index/Scripts/script.js"></script>
	<script language="javascript" src="<%=path %>/js/common.js"></script>
	<link href="<%=path %>/index/Themes/com/style.css" type="text/css" rel="stylesheet"></link>
	<script src='<%=basePath%>dwr/engine.js'></script>
     <script src='<%=basePath%>dwr/util.js'></script> 
     <script src='<%=basePath%>dwr/interface/subject.js'></script> 
    <script type="text/javascript">
      var xmlHttp;
      var url="";
      var delNode;
      //var nameArray=new Array();
    function lookMe(thisTag){
		var span = thisTag.getElementsByTagName('span')[0];
		span.style.display = 'block';
		thisTag.onmouseout = function(){
			span.style.display = 'none';
		}
	}

	function mouseMove(event){
		var xx=event.clientX + 5;
		var yy=event.clientY + 10;
		var obj = event.srcElement ? event.srcElement : event.target;
	        var span=obj.getElementsByTagName("span")[0];
		//span.style.left = xx + 'px';
		//span.style.top = yy + 'px';
	}


      function createXMLHttpRequest() {
			if (xmlHttp == null) {
	    		if (window.ActiveXObject) {
	    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    		}
	    		else if (window.XMLHttpRequest) {
	        		xmlHttp = new XMLHttpRequest();                
	    		}
    		}
	  }
			  
      function change(){
      		var orgId=document.form0.org.value;
      		var zhicheng=document.form0.zhicheng.value;
      		var name=document.form0.name.value;
      		//alert("org id is "+orgId+" zhicheng is "+zhicheng+" name is "+name);
            url="subject.htm?action=getResearchers&orgId="+orgId+"&zhicheng="+zhicheng+"&name="+escape(name);
     	    createXMLHttpRequest();
			xmlHttp.open("GET", url, true);
    		xmlHttp.onreadystatechange = callback;
    		xmlHttp.send(null);
      }
      
     /* function searchName(event){
      	if(event.keyCode==13) {
      	  alert("yes");	change();
      	}
      }*/
      function isAlreadyAdd(orgId){
          subject.checkSubjectOrg(orgId,callbackForAdd)
      
      }
      function callbackForAdd(message){
         if("2" == message){
         	alert("该单位已经添加到该课题中,不能重复添加!")
         	flag = false;
         }
      
      }
      function callback(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				clearResults();
    				parseMessage();
    			}
    		}
      }
      function researchersObj(){
         var dept = '&nbsp;';
         var zhicheng = '&nbsp;';
		 var major = '&nbsp;';
      }
      function parseMessage(){
      		var xmlDoc = xmlHttp.responseXML;
			var nodes = xmlDoc.getElementsByTagName('researcher');
			var res=document.getElementById("researchers");
			if(nodes!=null){
				var id="";
				var name="";
				var detail="";
				var tr;
				for(var i=0;i<nodes.length;i++){
					//id=nodes[i].firstChild.firstChild.nodeValue;
					//name=nodes[i].nextChild.firstChild.nodeValue;
					//detail=nodes[i].lastChild.firstChild.nodeValue;
					id=nodes[i].getElementsByTagName("id")[0].firstChild.nodeValue;
					name=nodes[i].getElementsByTagName("name")[0].firstChild.nodeValue;
					detail=nodes[i].getElementsByTagName("detail")[0].firstChild.nodeValue;
					var resObj =new researchersObj();
					if(nodes[i].getElementsByTagName("dept")[0].firstChild != null)
						resObj.dept = nodes[i].getElementsByTagName("dept")[0].firstChild.nodeValue;
					else resObj.dept="";
					if(nodes[i].getElementsByTagName("zhicheng")[0].firstChild != null)
						resObj.zhicheng = nodes[i].getElementsByTagName("zhicheng")[0].firstChild.nodeValue;
					else resObj.zhicheng="";
					if(nodes[i].getElementsByTagName("major")[0].firstChild != null)
						resObj.major = nodes[i].getElementsByTagName("major")[0].firstChild.nodeValue;
						else resObj.major="";
					if(i%12==0){
						tr = res.insertRow(res.rows.length);
					}
					
					addResN(id,name,detail,tr,resObj);					
					//addRes(id,name);
				
				}
			}			
      }
      function researcherChick(id){
	      if(id==null)
	      return ;
	      var cid =document.getElementById("researcher-"+id);
	      if(cid != null)
	      	if(cid.checked)cid.checked = false;
	      		else cid.checked = true;
      }
      function addResN(id,name,detail,parentNode,resObj){
      	var td= parentNode.insertCell();
      	td.innerHTML=
      	'<div onmouseover="lookMe(this);" class="lookMe" onmousemove="mouseMove(event);">'+
      	'<input type="checkbox" name="researcher" id="researcher-'+id+'" value="'+id+'"><font style="cursor: hand" onclick="researcherChick('+id+');">'+name+'</font>'+
      	'<span>'+
      	'姓名:'+name+
      	'<br>部门:'+resObj.dept+
      	'<br>职称:'+getZhicheng(resObj.zhicheng)+
      	'<br>专业:'+resObj.major+
      	'</span>'+
      	'</div>'; 
      }
      function getZhicheng(zhicheng){
         if("1" == zhicheng)
            return "正高";
         if("2" == zhicheng)
            return "副高";
         if("3" == zhicheng)
            return "中级";
         if("4" == zhicheng)
            return "初级";
         if("5" == zhicheng)
            return "其它"; 
         if("undefined" == zhicheng)
           return "";
      }
    
      function clearResults(){
         	var res=document.getElementById("researchers");
         	while(res.childNodes.length > 0) {
				res.removeChild(res.childNodes[0]);
			}
      }
      
   
      
      function addResearchers(){
        var researcherIds="";
        var trs=document.getElementsByName("researcher");
		for(var i=0;i<trs.length;i++){
				var checkb=trs[i];
				if(checkb.checked){
					researcherIds += checkb.value+";";
				}
		}
		if(researcherIds==""){
			alert("请选择至少一个科研人员");
			return;
		}
		//alert(researcherIds);
		var role=document.form0.role.value; 
		var orderNum=document.form0.orderNum.value;
		if(isNull(orderNum)){
		  alert("请输入人员顺序号");
		  return; 
		}
		
		if(!checkSeq(orderNum)){
			return;
		}
		url="subject.htm?action=addResearchers&subjectId=<%=subject.getId()%>&researcherIds="+researcherIds+"&role="+role+"&orderNum="+orderNum;
		//alert(url);
		createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
    	xmlHttp.onreadystatechange = callbackForAddResearcher;
    	xmlHttp.send(null);
      }
      
      function checkSeq(seq){
      	var exp = new RegExp("^[0-9]{1,4}$");
      	if(isNull(seq)){
      	 	alert("请输入顺序号");
      	}  
      	 if(seq!="" && !exp.test(seq)){
      		alert("请输入有效的顺序号，顺序号必须是整数");
      		return false;
      	}
      	return true;
      }
      
      function callbackForAddResearcher(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				var xmlDoc = xmlHttp.responseXML;
					var nodes = xmlDoc.getElementsByTagName('researcherAdded');
					var added=nodes[0].firstChild.nodeValue;
					if(added>0){					
    					alert("已成功添加"+added+"名科研人员");
    					window.location.replace(document.location.href);
      					//document.location.href ="subject.htm?action=gotoAddResearcher&subjectId="+id+"&page="+page;
    				}else{
      					var errMsg=xmlDoc.getElementsByTagName('errMsg')[0].firstChild.nodeValue;
      					alert(errMsg);
      				}
    			}
    	}
      }
      
      
      function delSubRes(subResId){
          if(confirm("确认要删除该人员吗？"))
          subject.delSubjectRes(subResId,callbackForDelSubRes);
      }
      
      function callbackForDelSubRes(data){
         alert(data);
         window.location.replace(window.location.href);
      } 
      
      function addNew(){
         var org=document.form0.org.value;
         var zhicheng=document.form0.zhicheng.value;
         var name=document.form0.name.value;
         var role=document.form0.role.value;
         var seq = document.form0.orderNum.value;  
         if(org==''){
	         alert("请选择此科研人员所属单位!");
	         return false;
         }
         if(zhicheng=='0'){
	         alert("请选择此科研人员的职称");
	         return false;
         }
         if(isNull(name)){
	         alert("请输入此科研人员的姓名!");
	         return false;
         }
       
		 if(!checkSeq(seq)){
			return;
		 }
		url="subject.htm?action=addNewResearcher&subjectId=<%=subject.getId()%>&name="+escape(name)+"&org="+org+"&zhicheng="+zhicheng+"&role="+role+"&orderNum="+seq;
		//alert(url);
		createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
    	xmlHttp.onreadystatechange = callbackForAddResearcher;
    	xmlHttp.send(null);
          
      }
      
      function chgOrd(subResId){
        if(!window.confirm("需要更改此科研人员的序号吗？","确定","取消"))	
            return;
        var name='resord'+subResId;
        var ord=document.getElementById(name).value;
        if (checkSeq(ord)) {
       	    url="subject.htm?action=chgOrder&subResId="+subResId+"&order="+ord;
      	    createXMLHttpRequest();
	    xmlHttp.open("GET", url, true);
    	    xmlHttp.onreadystatechange = callbackChgResOrd;
            xmlHttp.send(null);
      	}
      }
      
        function callbackChgResOrd(){
      	    if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
   				var xmlDoc = xmlHttp.responseXML;
                                alert(xmlDoc.getElementsByTagName('errMsg')[0].firstChild.nodeValue);
      		       }
    	    }
    	    refresh();
    	}

	    function changeOrder(subResId){
	    	var name='resord'+subResId;
       	    var ord=document.getElementById(name).value;
	         if (checkSeq(ord)) {
	            subject.changeOrder(subResId,ord,callBackChangeOrder);
	         }
	    }
	    function callBackChangeOrder(message){
	        
	        alert(message); 
	        refresh();
	        
	    }

        function refresh(){
                window.location.replace(document.location.href);
        }
        
        function isModifySubject(id,status){
            if('1' == status){
               if(confirm("确认要赋予该参与人员修改课题的权限吗？"))
                     subject.assignAuth(id,status,callBackForUpdateAuth);
            }  
            if('0' == status){
               if(confirm("确认要取消该参与人员修改课题的权限吗？"))
                     subject.assignAuth(id,status,callBackForUpdateAuth);
            }    
        
        }
        function callBackForUpdateAuth(data){
            alert(data);
            refresh();
        }
    </script>
    <style type="text/css">
.lookMe{
	color:#0099FF;
	cursor:pointer;
}
.lookMe span{
	display:none;
	position:absolute;
	/*left:50px;
	top:20px;*/
	padding:5px;
	width:200px;
	z-index:10000000000;
	color:#CC3300;
	background:#FFFADC;
	border:1px solid #CC6600;
}
</style>
  </head>
  <body class="rtm_body" onload="change();">
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr>
            <td>
                <div class="content">
                    <table width="*" cellpadding="0"  cellspacing="1" class="dg_borderstyle" id="Table4">
                        <tr>
                            <td class="dg_headerstyle" width="15%">
                               	序号</td>
                            <td class="dg_headerstyle" width="20%">
                                 姓名</td>
                            <td class="dg_headerstyle" width="35%">
                                 职称</td>
                            <td class="dg_headerstyle">
                                角色</td>
 							<td class="dg_headerstyle">
                                是否可修改课题</td>  
                            <td class="dg_headerstyle">
                                删除</td> <% //如果课题状态为1 那么不允许删除 稍后完善  %>
                        </tr>
                        <% 
                        Set reSet = subject.getSubRes();
                        
                        if(null != reSet && reSet.size()>0){ 
	                        Iterator iterator = reSet.iterator(); 
	                        int i =1;
	                        while(iterator.hasNext()){
	                        	 SubRes subRes = (SubRes)iterator.next();
	                        	 if(null == subRes.getRes())
	                        		 continue;
	                        %>
	                        <tr class="dg_alternatingitemstyle">
	                            <td> <input type="text" id="resord<%=subRes.getId() %>" class="com_textbox" size="5" value ="<%=subRes.getOrderNum() %>"/>
	                         <a href="javaScript:changeOrder('<%=subRes.getId() %>');">改变序号</a></td>
	                            <td>
	                                <%=subRes.getRes().getName() %>
	                            </td>
	                            <td>
	                                 <%
	                                 if(Constants.researcher_zhenggao.equals(subRes.getRes().getZhicheng()+""))
	                                     out.println("正高");
	                                  if(Constants.researcher_fugao.equals(subRes.getRes().getZhicheng()+""))
	                                     out.println("副高");
	                                  if(Constants.researcher_zhongji.equals(subRes.getRes().getZhicheng()+""))
	                                     out.println("中级");
	                                  if(Constants.researcher_diji.equals(subRes.getRes().getZhicheng()+""))
	                                     out.println("初级");
	                                   if(Constants.researcher_zhich_others.equals(subRes.getRes().getZhicheng()+""))
	                                     out.println("其它"); %>
	                            </td>
	                            <td>
	                               <%
	                                  if(Constants.researcher_role_chargeman.equals(subRes.getResRole()+"")){
	                                  		out.println("负责人");
	                                  }
	                                   if(Constants.researcher_role_participant.equals(subRes.getResRole()+"")){
	                                  		out.println("参与人员");
	                                  }  %>
	                            </td>
	                             <td align="center">
	                               <% 
	                                   if(Constants.researcher_role_participant.equals(subRes.getResRole()+"")){
	                                	   if("1".equals(subRes.getIsModify()+"")){%>
	                                	   <a href="javaScript:isModifySubject('<%=subRes.getId() %>','0');">不可修改课题信息</a>
	                                       <%}else{ %>
	                                  		<a href="javaScript:isModifySubject('<%=subRes.getId() %>','1');">可修改课题信息</a>
	                                  		<%} %>
	                                  <%} %>
	                            </td> 
	                            <td align="center">
	                                <img style="cursor:hand" src="<%=imagePath %>Themes/com/68design.net_registration1_(delete)_16x16.gif" onclick="javaScript:delSubRes(<%=subRes.getId() %>);"
	                                    alt="删除" />
	                            </td> 
	                        </tr> 
                        <%}}else{ %>
                          <tr class="dg_alternatingitemstyle"> 
                            <td align="center" colspan=6>
                                <font color="red">没有找到记录</font>
                            </td> 
                        </tr>  
                        <%} %>
                    </table>  
                </div>
            </td>
        </tr>
    </table>
  
	
 <table cellpadding="0" cellspacing="0" width="100%" height="100%" id="Table8">
 <form name="form0">
     	<input type="hidden" name="subjectId" value="${subjectId}"/>
        <tr>
            <td valign="top" height="100%">
                <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" id="Table11">
                    <tr>
                        <td valign="top" >
                            <!------------------ 标题栏 ------------------>
                            <!--- 标题 ---->
                            <table class="idt_tle_mg" cellspacing="0" cellpadding="0" width="100%">
                                <tr >
                                    <td class="idt_tle_i">
                                        <img id="imgForm6" src="<%=basePath%>index/Themes/com/idt_tle_o.gif"></td>
                                    <td class="idt_tle_f1">
                                        添加科研人员</td>
                                </tr>
                            </table>
                            <!--- 内容 --->
                            <div id="tdForm">
                                <table border="0" cellpadding="0" cellspacing="1" width="100%" class="idt_fim_mg">
                                    <tr>
                                        <td class="fim_l" width="15%">
                                           所在单位</td>
                                        <td class="fim_r" width="35%">
	                                        <select name="org"  onchange="change();"  style="width:100%"> 
				    					<%   
				    					     Iterator iterator02 = orgList.iterator();
					    					 while(iterator02.hasNext()){
					    					 	Organization org = (Organization)iterator02.next();
					    					 	Long key = org.getId();
				    					 %> 
				    					 	<option value="<%=key %>" <%if(key == currentOrgId)out.print("selected"); %>><%=org.getName() %></option>
				    					 <%} %>
				    			    </select>
                                        </td>
                                         <td class="fim_l" width="15%">
                                           职称</td>
                                        <td class="fim_r" >
	                                       <select name="zhicheng" onchange="change();" style="width:80%">
												<option value="0">-------请选择--------</option>
												<option value="1">正高</option>
												<option value="2">副高</option>
												<option value="3">中级</option>
												<option value="4">初级</option>
												<option value="5">其它</option>
											</select>
                                            </td>
                                    </tr>
                                    <tr>
                                        <td class="fim_l" width="15%">
                                          姓名</td>
                                        <td class="fim_r"  id="departments" colspan="3" >
                                       		<input type="text" name="name" class="com_textbox" style="width:40%" onkeyup="change();">&nbsp;&nbsp; <font color=red><b>注：输入姓名，可在以下列表中查询科研人员</b></font>
                                         </td>
                                    </tr>
                                    <tr>
                                        <td class="fim_l">备选人</td>
                                        <td class="fim_r" colspan="3">
	                                       <TABLE id="researchers">
	                                       </TABLE>
                                            </td>
                                      </tr>
                                      <tr>
                                        <td class="fim_l">
                                            人员角色</td>
                                        <td  class="fim_r" colspan="3">
                                           <select name="role"><option value="<%=Constants.researcher_role_chargeman %>">负责人</option><option value="<%=Constants.researcher_role_participant %>">参与人员</option></select>
 											&nbsp;&nbsp;顺序号：<input name="orderNum" class="com_textbox"   type="text" size="5" value=""/>（填一整数，如1，2等）
                                        &nbsp;&nbsp;
                                        <input type="button" value="添加到课题"  class="com_textbox"  onclick="addResearchers();">
                                           </td>
                                    </tr>
                                    <!-- tr>
                                    <td colspan="4">
									  <font color="red">若科研人员在名单中找不到，请在上述姓名输入框中输入人员姓名并点击：</font>
										<input type="button"  name="sss" value="添加人员并添加到课题"  class="com_textbox"  onclick="addNew();"/>
									</td>
									</tr-->
                                </table>
                                 <table cellspacing="0" cellpadding="0" width="100%" class="idt_bottom_mg" border="0">
                                    <tr>
                                        <td align="center">
                                           <!--  <input  class="com_btn2" id="Button1"  onclick="" type="button"   value="上一步">
                                            &nbsp; &nbsp;
                                             <input  class="com_btn2" id="Button1"  onclick="" type="button" value="下一步">-->
                                        </td>
                                    </tr>
                                </table>
                            </div>
                </table></form>
  </body>
</html>
